#coding:utf-8
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import functools
from django.shortcuts import render_to_response
from django.http import HttpResponse 
from django.contrib.auth import authenticate, login
from django.contrib.auth import logout
from testlogin.models import MySiteProfile

if __name__ == "__main__" and False:
    from django.contrib.auth.models import User
    user = User.objects.create_user(username="name1", email="", password="key")
    user.save()
    MySiteProfile(user=user,msn="msn").save()
    profile = user.get_profile()

from django.contrib.auth.models import User
user = User.objects.get(username="name1")

from django.contrib.auth.models import Group, Permission
g = Group.objects.get(name="test")

user.groups.add(g)
user.save()

g.permissions = [Permission.objects.get(name="Can add message")]
g.save()

user.user_permissions  = [Permission.objects.get(name="Can delete message")]
user.save()

#g.permissions.add(Permission.get())
#g.save()
#myuser.groups.add(group, group, ...)
#myuser.groups.remove(group, group, ...)
#myuser.groups.clear()
#myuser.user_permissions = [permission_list]
#myuser.user_permissions.add(permission, permission, ...)
#myuser.user_permissions.remove(permission, permission, ...)
#myuser.user_permissions.clear()

http://wiki.woodpecker.org.cn/moin/DjangoAuth-ZH


profile -- 
http://ddkangfu.blog.51cto.com/311989/118699
1.settings.py AUTH_PROFILE_MODULE = 'testlogin.mysiteprofile'
2.建表
class MySiteProfile(models.Model):
    # This is the only required field
    user = models.ForeignKey(User, unique=True)
    msn = models.CharField(maxlength=100, blank=True)
3.from django.contrib.auth.models import User
user = User.objects.create_user(username="name1", email="", password="key")
user.save()
MySiteProfile(user=user,msn="msn").save()
profile = user.get_profile()



group --

from django.contrib.auth.models import Group
Group(name="test").save()





